package seap.dao;

import java.util.Iterator;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import seap.beans.LancarMadeiraCorteBean;

public class LancarMadeiraCorteDAO {
	
	protected Session session;
	protected Transaction tx;
	private static List<LancarMadeiraCorteBean> box;
    
    public LancarMadeiraCorteDAO() throws HibernateException {
        HibernateFactory.buildSessionFactory();
    }

    public Session getSession() {
        return session;
    }

    public Transaction getTx() {
        return tx;
    }
    
    public String salvar(LancarMadeiraCorteBean obj){
        try {
        	session = HibernateFactory.openSession();
            tx = session.beginTransaction();
            session.save(obj);
            tx.commit();
            return "sucesso";
        } catch (HibernateException e){
        	HibernateFactory.rollback(tx);
        	return "falha";
        } finally {
            HibernateFactory.close(session);
        }
    }

    @SuppressWarnings("unchecked")
	public LancarMadeiraCorteBean localizar(int id){
    	
    	LancarMadeiraCorteBean obj = null;
        Iterator iter = box.iterator();
        
        while(iter.hasNext()){
        	obj = (LancarMadeiraCorteBean) iter.next();
        	if(obj.getId() == id){
        		break;
        	}
        }
        return obj;
    }
    
    @SuppressWarnings("unchecked")
	public List localizarTodos(Class clazz){
    	List objs = null;
        try {
        	session = HibernateFactory.openSession();
            tx = session.beginTransaction();
            Query q = session.createQuery("from " + clazz.getName());
            objs = q.list();
            tx.commit();
        } catch (HibernateException e) {
        	HibernateFactory.rollback(tx);
        } finally {
            HibernateFactory.close(session);
        }
        return objs;
    }

    @SuppressWarnings("unchecked")
	public void remover(LancarMadeiraCorteBean obj) {
        try {
        	session = HibernateFactory.openSession();
            tx = session.beginTransaction();
            session.delete(obj);
            tx.commit();
        } catch (HibernateException e) {
        	HibernateFactory.rollback(tx);
        } finally {
            HibernateFactory.close(session);
        }
    }
    
    public void salvarOuAtualizar(LancarMadeiraCorteBean obj) {
        try {
        	session = HibernateFactory.openSession();
            tx = session.beginTransaction();
            session.saveOrUpdate(obj);
            tx.commit();
        } catch (HibernateException e) {
        	HibernateFactory.rollback(tx);
        } finally {
            HibernateFactory.close(session);
        }
    }
    
    public void handleException(HibernateException e){
    	HibernateFactory.rollback(tx);
    	return;
    }
}